import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GameWin extends JFrame {
    //分数
    public static int score=0;
    public static int dragonBallCount=0;

    //游戏状态 0为未开始，1为游戏中，2为暂停，3为失败，4为通过,5为失败后重启
    public static int state=0;

    //窗口宽高
    int winWidth=1920;
    int winHeight=1080;

    //人物对象位置
    RW rw=new RW(GameUtils.Img,10,850,this);

    //食物
    FoodObj foodObj=new FoodObj().getFood();
    //设置背景图片
    Image bjImg=new ImageIcon("img/bj.png").getImage();

    public void launch(){
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(winWidth,winHeight);
        //设置窗口的位置在屏幕上居中
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("吃龙珠");

        //键盘事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode()==KeyEvent.VK_SPACE){
                    switch (state){
                        //未开始
                        case 0:
                            state=1;
                            break;
                        case 1:
                            //游戏中
                            state=2;
                            repaint();
                            break;
                        case 2:
                            //游戏暂停
                            state=1;
                            break;
                        default:
                            break;

                    }
                }
            }
        });
        while (true){
            if (state==1){
                //游戏中才可以调用
                repaint();
            }

            try{
                //1秒1000毫秒
                Thread.sleep(100);
            } catch (InterruptedException e){
                e.printStackTrace();
            }

        }

    }
    @Override
    public void paint(Graphics g){
        g.setColor(Color.gray);
        g.fillRect(0,0,winWidth,winHeight);
        //背景绘制
        g.drawImage(bjImg,0,0,this);
        //绘制人物
        rw.paintSelf(g);
        //食物绘制
        foodObj.paintSelf(g);
        //分数绘制
        GameUtils.drawWord(g,score+"分",Color.white,50,1600,100);
        GameUtils.drawWord(g,dragonBallCount+"颗龙珠",Color.yellow,50,1600,200);
        //绘制提升语
        prompt(g);
    }
    //绘制提示语
    void prompt(Graphics g){
        //未开始
        if(state==0){
            GameUtils.drawWord(g,"按下空格开始游戏",Color.YELLOW,60,750,600);
        }
        //暂停时
        if(state==2){
            GameUtils.drawWord(g,"按下空格继续游戏",Color.YELLOW,60,750,600);
        }
        //失败
        if(state==3){
            GameUtils.drawWord(g,"游戏失败",Color.YELLOW,60,750,600);
        }
        //通关
        if(state==4){
            GameUtils.drawWord(g,"达成条件！游戏通关",Color.YELLOW,60,750,600);
        }
    }


    public static void main(String[] args) {
        GameWin gameWin=new GameWin();
        gameWin.launch();
    }
}
